package org.dragonnova.meetingclient.core.message;

import org.dragonnova.meetingclient.core.net.SocketUtils;
import org.dragonnova.meetingclient.utils.LogUtil;

import java.nio.ByteBuffer;

/**
 * * 指令格式：
 * ****************************** *
 * **   HEADER 2 bytes         ** *
 * **   TYPE  1 bytes          ** *
 * **   LENGTH 4 bytes         ** *
 * **   DATA   ? bytes         ** *
 * **   CS     1 bytes         ** *
 * **   END    2 bytes         ** *
 * ****************************** *
 */

/**
 * Created by Administrator on 2015/11/17 22:12.
 * Author: songxy
 * Email: thinkdata@163.com
 */
public class GroupMessage extends org.dragonnova.meetingclient.core.message.Message implements Cloneable {

    /**
     *
     */
    private final static int DATA_LENGTH = 150;
    /**
     * 数据域变长
     */
    private int packetLength;
    /**
     * 头部
     */
    private byte[] packetHeader;
    /**
     * 类型
     */
    private byte[] packetType;
    /**
     * 数据域
     */
    private byte[] packetData;
    /**
     * 校验值
     */
    private byte[] packetCS;
    /**
     * 尾部
     */
    private byte[] packetEnd;

    public GroupMessage() {
        this.packetHeader = new byte[]{(byte) 0xEE, (byte) 0xFF};
        this.packetType = new byte[1];
        this.packetCS = new byte[]{(byte) 0xEE, (byte) 0xEE};
        this.packetEnd = new byte[]{(byte) 0xFF, (byte) 0xFF};
    }

    public void checksum() {
        if (packetHeader[0] == (byte) 0xEE && packetHeader[1] == (byte) 0xFF
                && packetCS[0] == (byte) 0xEE && packetCS[1] == (byte) 0xEE
                && packetEnd[0] == (byte) 0xFF && packetEnd[1] == (byte) 0xFF) {
            LogUtil.ddd("receive message correct");
        } else {
            LogUtil.ddd("group message format incorrect");
            //PromptUtil.poolPlay(R.raw.group_message_error);
        }
    }

    @Override
    public byte[] encode() {
        if (packetData == null) {
            throw new RuntimeException("the packetData is null!");
        }
        ByteBuffer bb = ByteBuffer.allocate(totalLength());
        bb.put(this.packetHeader, 0, this.packetHeader.length);
        bb.put(this.packetType, 0, this.packetType.length);
        bb.putInt(this.packetLength);
        bb.put(this.packetData, 0, this.packetLength);
        bb.put(this.packetCS, 0, this.packetCS.length);
        bb.put(this.packetEnd, 0, this.packetEnd.length);
        checksum();
        return bb.array();
    }

    @Override
    public Message decode(byte[] bytes) {
        LogUtil.eee("the packetData total length = " + totalLength());
        ByteBuffer msgBuffer = ByteBuffer.wrap(bytes);
        this.packetHeader = new byte[2];
        msgBuffer.get(this.packetHeader, 0, this.packetHeader.length);
        this.packetType = new byte[1];
        msgBuffer.get(this.packetType, 0, this.packetType.length);
        this.packetLength = msgBuffer.getInt();
        this.packetData = new byte[this.packetLength];
        msgBuffer.get(this.packetData, 0, this.packetLength);
        this.packetCS = new byte[2];
        msgBuffer.get(this.packetCS, 0, this.packetCS.length);
        this.packetEnd = new byte[2];
        msgBuffer.get(this.packetEnd, 0, this.packetEnd.length);
        checksum();
        return this;
    }


    public byte[] getStart() {
        return this.packetHeader;
    }

    @Override
    public byte[] getAction() {
        return packetType;
    }

    @Override
    public void setAction(byte[] action) {
        this.packetType = action;
    }

    @Override
    public int getPacketDataLength() {
        return packetLength;
    }

    @Override
    public void setPacketDataLength(int data1) {
        this.packetLength = data1;
    }

    @Override
    public byte[] getPacketData() {
        return packetData;
    }

    @Override
    public void setPacketData(byte[] data2) {
        this.packetData = data2;
    }

    public byte[] getEnd() {
        return this.packetEnd;
    }

    public byte[] getCS() {
        return packetCS;
    }

    public int totalLength() {
        //根据数据域的长度大小赋值
        int dataLen;
        if (getPacketDataLength() == 0) {
            dataLen = DATA_LENGTH;
        } else {
            dataLen = getPacketDataLength();
        }
        return this.packetHeader.length + this.packetType.length
                + 4 + this.packetCS.length + this.packetEnd.length + dataLen;
    }

    @Override
    public String toString() {
        return "Header(2)=[ " + SocketUtils.byte2HexString(packetHeader)
                + " ] Type(1)=[ " + SocketUtils.byte2HexString(packetType)
                + " ] Data Length(4)=[ " + SocketUtils.byte2HexString(packetHeader)
                + " ] Data Context=[ " + new String(packetData)
                + " ] CS=[ " + SocketUtils.byte2HexString(packetCS)
                + " ] End=[ " + SocketUtils.byte2HexString(packetEnd) + "] .";
    }
}
